<?php
namespace Events\model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Where;
use Zend\Db\ResultSet\ResultSet;

class RegisterTable
{
    protected $tableGateway;
    
    function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway=$tableGateway;
    }
    
    public function fetchAll($paginated=false)
    {
        if ($paginated) {
            $select = new Select('register');
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Register());
            $paginatorAdapter = new DbSelect(
                $select,
                $this->tableGateway->getAdapter(),
                $resultSetPrototype
            );
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
    
    public function getParticipatedEvent($id)
    {
        $rowset = $this->tableGateway->select(array('FK_PARTICIPANT_ID' => $id));
        $row = $rowset->current();
        /* if (!$row) {
            throw new \Exception("Could not find Participant ID=$id");
        } */
        return $row;
    }
    
    public function getParticipatedEvents($id)
    {
        $rowset = $this->tableGateway->select(array('FK_PARTICIPANT_ID' => $id));
         if (!$rowset) {
         throw new \Exception("Could not find Participant ID=$id");
        }        
        return $rowset;
    }
    
    public function getEventParticipant($event_id,$paginated=false)
    {
         if ($paginated) {
             $select = new Select('participant');
             $select->join('register','register.FK_PARTICIPANT_ID=participant.ID',array(), 'left');
             $where=new Where();
             $where->equalTo('fk_event_id', $event_id) ;
             $select->where($where);
             
             $paginatorAdapter = new DbSelect(
                 $select,
                 $this->tableGateway->getAdapter()
             );
             $paginator = new Paginator($paginatorAdapter);
             return $paginator;
         }
         
         $rowset = $this->tableGateway->select(function (Select $select) use ($event_id){
             $select->where(array('FK_EVENT_ID'=> $event_id));
         }); 
         
         return $rowset; 
    }
    
    public function getRegistrations($event_id){
        $rowset = $this->tableGateway->select(function (Select $select) use ($event_id){
            $select->where(array('FK_EVENT_ID'=> $event_id));
        });
        return $rowset->count();
    }
    
    public function saveRegister(Register $register)
    {
        $data = array(
            'FK_PARTICIPANT_ID' => $register->FK_PARTICIPANT_ID,
            'FK_EVENT_ID' => $register->FK_EVENT_ID
        );
        
        $event_id = $register->FK_EVENT_ID;
        $participant_id=$register->FK_PARTICIPANT_ID;
        
        if (!($this->getParticipatedEvent($event_id) == $event_id && $this->getEventParticipant($participant_id)==$participant_id)) {
            $this->tableGateway->insert($data);
        }else{
               throw new \Exception('record exist');
        }
        
    } 
}

?>